/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.expert.dao.SolutionFaceProductMixDao;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductMix;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductStep;

/**
 *  方案产品步骤Service
 * @author liujiang
 * @version 2013-08-19
 */
@Component
@Transactional(readOnly = true)
public class SolutionFaceProductMixService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SolutionFaceProductMixService.class);
	
	@Autowired
	private SolutionFaceProductMixDao solutionFaceProductMixDao;
	
	public SolutionFaceProductMix get(Long id) {
		return solutionFaceProductMixDao.findOne(id);
	}
	
	public List<SolutionFaceProductMix> getByStep(long stepId,
			long locationSeason, long storeTypeId){
		return solutionFaceProductMixDao.findByStep(stepId, locationSeason, storeTypeId);
	}
	
	public Page<SolutionFaceProductMix> find(Page<SolutionFaceProductMix> page, SolutionFaceProductMix solutionFaceProductMix, Long stepId) {
		DetachedCriteria dc = solutionFaceProductMixDao.createDetachedCriteria();
		if (stepId != null){
			dc.add(Restrictions.eq("solutionFaceProductStep.id", stepId));
		}
		dc.addOrder(Order.asc("id"));
		return solutionFaceProductMixDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(SolutionFaceProductMix solutionFaceProductMix) {
		solutionFaceProductMixDao.save(solutionFaceProductMix);
	}
	
	@Transactional(readOnly = false)
	public void delete(SolutionFaceProductMix solutionFaceProductMix) {
		solutionFaceProductMixDao.delete(solutionFaceProductMix);
	}
	
	//获取当前记录排第几位
	public int getIndexById(Long currentId, Long solutionId) {
		DetachedCriteria dc = solutionFaceProductMixDao.createDetachedCriteria();
		dc.add(Restrictions.le("id", currentId));
		if(solutionId !=null) {
			dc.add(Restrictions.eq("solutionFaceProductStep.id", solutionId));
		}
		dc.addOrder(Order.asc("id"));
		return (int)solutionFaceProductMixDao.count(dc);
	}
}
